package com.example.floatingpopulation.controller;

import cn.hutool.crypto.SecureUtil;
import com.example.floatingpopulation.common.CaptchaConfig;
import com.example.floatingpopulation.common.Result;
import com.example.floatingpopulation.controller.dto.forgetPasswordDTO;
import com.example.floatingpopulation.controller.request.LoginRequest;
import com.example.floatingpopulation.controller.request.RegisterRequest;
import com.example.floatingpopulation.controller.request.UserPageRequest;
import com.example.floatingpopulation.entity.Admin;
import com.example.floatingpopulation.entity.Password;
import com.example.floatingpopulation.entity.User;
import com.example.floatingpopulation.service.UserService;
import com.example.floatingpopulation.utils.VerificationCodeStore;
import com.wf.captcha.utils.CaptchaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    private static final String PASS_SALT = "qiu";
    @Autowired
    UserService userService;

    @Autowired
    private VerificationCodeStore verificationCodeStore;

    @PostMapping("/login")
    public Result<String> login(@RequestBody LoginRequest request, @RequestParam String key, HttpServletRequest httpRequest) {
        // 判断验证码正确性
        if (!request.getVerCode().toLowerCase().equals(CaptchaConfig.CAPTCHA_MAP.get(key))){
            // 不相等，验证不通过，清除前台的http请求内容
            CaptchaUtil.clear(httpRequest);
            return Result.error("验证码错误");
        }
        // 清除这次存储的验证码key
        CaptchaConfig.CAPTCHA_MAP.remove(key);
        return Result.success(userService.login(request));
    }

    @PostMapping("/register")
    public Result<String> register(@RequestBody RegisterRequest request, @RequestParam String key, HttpServletRequest httpRequest) {
//        // 判断验证码正确性
//        if (!request.getVerCode().toLowerCase().equals(CaptchaConfig.CAPTCHA_MAP.get(key))){
//            // 不相等，验证不通过，清除前台的http请求内容
//            CaptchaUtil.clear(httpRequest);
//            return Result.error("验证码错误");
//        }
//        // 清除这次存储的验证码key
//        CaptchaConfig.CAPTCHA_MAP.remove(key);
//        if(!request.getPassword().equals(request.getConfirmPassword())) {
//            return Result.error("输入的两个密码不相同，请重新注册！");
//        }
        if (!verificationCodeStore.getCode(request.getEmail()).equals(request.getVerCode())){
            return Result.error("验证码输入错误！");
        }
        if(!request.getPassword().equals(request.getConfirmPassword())) {
            return Result.error("输入的两个密码不相同，请重新输入！");
        }
        return Result.success(userService.register(request));
    }

    @PostMapping("/save")
    public Result<String> save(@RequestBody User user) {
        userService.save(user);
        return Result.success();
    }

    @PutMapping("/update")
    public Result<String> update(@RequestBody User user) {
        userService.update(user);
        return Result.success();
    }


    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Integer id){
        userService.deleteById(id);
        return Result.success();
    }


    @GetMapping("/list")
    public Result<String> list(){
        List<User> users = userService.list();
        return Result.success(users);
    }

    @GetMapping("/{id}")
    public Result<String> getById(@PathVariable Integer id){
        User obj = userService.getById(id);
        return Result.success(obj);
    }

    @GetMapping("/page")
    public Result<String> page(UserPageRequest userPageRequest){
        return Result.success(userService.page(userPageRequest));
    }

    @PutMapping("/updatePassword")
    public Result<String> updatePassword(@RequestBody Password password){
        User user = userService.getById(Integer.valueOf(password.getId()));
        if (!SecureUtil.md5(password.getPassword() + PASS_SALT).equals(user.getPassword())){
            return Result.error("原始密码错误，修改密码失败");
        }else {
            userService.updatePassword(password);
        }
        return Result.success();
    }

    @PutMapping("/resetPassword")
    public Result<String> resetPassword(@RequestBody User user){
        userService.resetPassword(user);
        return Result.success();
    }

    @PostMapping("/resetPassword/forget")
    public Result<String> resetPasswordForget(@RequestBody forgetPasswordDTO dto){
        if (!verificationCodeStore.getCode(dto.getEmail()).equals(dto.getVerCode())){
            return Result.error("验证码输入错误！");
        }
        userService.resetPasswordForget(dto);
        return Result.success();
    }
}
